package controller.services.impl;

import controller.services.JLayerManagerService;
import controller.sound.SoundJLayer;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Port;
import javax.sound.sampled.UnsupportedAudioFileException;
import model.PlayModeTypes;
import model.mp3.MP3Info;
import model.mp3.MP3Object;
import model.mp3.impl.MP3InfoImpl;
import org.tritonus.share.sampled.TAudioFormat;
import org.tritonus.share.sampled.file.TAudioFileFormat;

public class JLayerManagerServiceImpl implements JLayerManagerService {

    private SoundJLayer soundToPlay;
    private File mp3File = null;

    @Override
    public void loadFile(File mp3File) {
        this.mp3File = mp3File;
        soundToPlay = new SoundJLayer(mp3File);
    }

    @Override
    public void play() {
        System.out.println("Accesing jlayer service play on ");
        if (mp3File != null) {
            soundToPlay.play();
            
        }
    }

    @Override
    public void stop() {
        soundToPlay.pause();
    }

    @Override
    public void pause() {
        //TODO: add code here
    }

    @Override
    public void setVolume(Float gain) {

        Line.Info source = Port.Info.SPEAKER;
        //        source = Port.Info.LINE_OUT;
        //       source = Port.Info.HEADPHONE;

        if (AudioSystem.isLineSupported(source)) {
            try {
                Port outline = (Port) AudioSystem.getLine(source);
                outline.open();
                FloatControl volumeControl = (FloatControl) outline.getControl(FloatControl.Type.VOLUME);
                System.out.println("       volume: " + volumeControl.getValue());
                volumeControl.setValue(gain);
                System.out.println("   new volume: " + volumeControl.getValue());

            } catch (LineUnavailableException ex) {
                System.err.println("source not supported");
                ex.printStackTrace();
            }
        }
    }

    @Override
    public void mute() {
        this.setVolume(0.00f);
        
    }
    

    @Override
    public void playNextSong() {
        //TODO: add code here
    }

    @Override
    public void playPreviousSong() {
        //TODO: add code here
    }

    @Override
    public void setPlayMode(PlayModeTypes playMode) {
        //TODO: add code here
    }

    @Override
    public void setPlaylist(List<MP3Object> playList) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void setPreset() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public MP3Info getSongInfo() {
        MP3Info mp3Info = new MP3InfoImpl();
        if (mp3File != null) {
            try {
                AudioFileFormat baseFileFormat = null;
                AudioFormat baseFormat = null;
                baseFileFormat = AudioSystem.getAudioFileFormat(mp3File);
                baseFormat = baseFileFormat.getFormat();
                // TAudioFileFormat properties
                if (baseFileFormat instanceof TAudioFileFormat) {
                    Map properties = ((TAudioFileFormat) baseFileFormat).properties();

                    String author = (String) properties.get("author");
                    String title = (String) properties.get("title");
                    String album = (String) properties.get("albun");
                    Long duration = (Long) properties.get("duration");
                    
                    if (author!= null) {
                        mp3Info.setArtist(author);
                    }
                    if (title != null) {
                        mp3Info.setTrackName(title);
                    }
                    if (duration != null) {
                        mp3Info.setLength(duration);
                    }
                }

            } catch (UnsupportedAudioFileException ex) {
                Logger.getLogger(JLayerManagerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(JLayerManagerServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return mp3Info;

    }

}
